package cn.sskxyz.security.authorization.web.controller;

import cn.sskxyz.mybatis.mode.Page;
import cn.sskxyz.security.authorization.service.UserService;
import cn.sskxyz.security.core.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/user/list")
    public Page<List<User>> getUserList() {
        return userService.queryUserList(1, 10);
    }

    @PostMapping("/user/add")
    public void addUser(@RequestBody User user) {
        userService.addUser(user);
    }

    @GetMapping("/user")
    public Object getUserInfo(Authentication authentication) {
        return authentication.getPrincipal();
    }

    @PostMapping(value = "/permission")
    public ResponseEntity auth(String module, String uri, long userId) {
        boolean hasPermission = userService.checkPermission(module, uri, userId);
        if (hasPermission) {
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.status(HttpStatus.MOVED_PERMANENTLY).build();
    }
}
